#include <cstdint>
#include <cstdio>
#include <gtest/gtest.h>
#include "hls_protocol_operation.h"
#include "http_protocol/http_protocol_msg.h"
#include "hls_protocol/hls_m3u8_message.h"
#include "loop_module/loop.h"
#include "protocol_connection.h"
#include "protocol_operation.h"

TEST(HlsProtocolOperation, Init)
{
	
}

TEST(HlsProtocolOperation, TestM3U8Url)
{
	std::string url = "https://nasaplus.akamaized.net/output/14957-3.m3u8";
	Loop loop;
	HlsProtocolOperation operation;
	ProtocolConnection   connection(&loop, &operation);

	connection.SetConnectionFn([](ProtocolConnection * conn, int result){

		printf("connect to server result : %d.\n", result);
		if (result >= 0)
		{
			auto host = conn->ProtocolData();
			HttpRequest request;
			request.AddHeaderLine("GET", host.m_uri, "HTTP/1.1");
			request.AddHeaderSign("Connection", "keep-alive");
			request.AddHeaderSign("Accept", "*/*");
			request.AddHeaderSign("Host", host.m_host);
			std::string content;
			request.String(content);
			conn->SendMessage((const uint8_t*)content.c_str(), content.size());
		}
	});
	connection.SetMessageFn([](ProtocolConnection * conn, ProtocolOperation * op){

		auto hls_op = (HlsProtocolOperation*)op;
		if (hls_op->OpSuccess() >= 0)
		{
			auto hls_msg = hls_op->M3U8Msg();
			printf("get hls msg success.\n");
			auto uri_array = hls_msg->UriIndex();
			for (auto & item : uri_array)
			{
				printf("%s.\n", item.m_index_uri.c_str());
			}
		}
		else
		{
			printf("check hls op success fail.\n");
		}
		conn->CloseConnection();
	});
	connection.InitServerUrl(url);
	loop.Run();
}
